package erfgame.frontend.desktop.controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import erfgame.core.event.queue.memory.InMemoryEventQueue;
import erfgame.core.world.World;
import erfgame.core.world.WorldDAO;
import erfgame.core.world.WorldDAOManager;
import erfgame.core.world.WorldEventQueueRunner;

import erfgame.frontend.desktop.view.ViewActionListener;
import erfgame.frontend.desktop.view.terrain.GameTerrainView;
import erfgame.frontend.desktop.view.terrain.iso.IsometricTerrainView;
import erfgame.frontend.desktop.view.world.list.ListWorldsView;

public class SelectViewWorldController extends AbstractController {
	
	private WorldDAOManager terrainDAOManager;
	
	private Container container;
	private Object constraints;
	
	private ListWorldsView worldsView;
	
	private World currentWorld;
	
	private GameTerrainView terrainView;
	
	public SelectViewWorldController( 
			WorldDAOManager terrainDAOManager, 
			GameTerrainView terrainView ) {
		this.terrainView = terrainView;
		this.terrainDAOManager = terrainDAOManager;
		
		terrainView.setCloseActionListener( new ActionListener() {
			
			public void actionPerformed(ActionEvent e) {
				fireControllerCompleted();
			}
		});
		
		this.worldsView = new ListWorldsView();
		this.worldsView.addViewActionListener( new ViewActionListener() {

			public void actionPerformed(Object source, int actionType, Object operation) {
				switch( actionType ) {
				case ListWorldsView.ACTION_TYPE_CANCEL:
					fireControllerCompleted();
					break;
				case ListWorldsView.ACTION_TYPE_WORLD_SELECTED:
					showWorld( (World)operation );
					break;
				case ListWorldsView.ACTION_TYPE_WORLD_DELETE:
					deleteWorld( (World)operation );
					break;
				}
			}
		});
	}
	
	private void deleteWorld( World world ) {
		WorldDAO worldDAO = terrainDAOManager.getWorldDAO(); 
		worldDAO.deleteWorld( world.getWorldId() );
		List<World> worlds = worldDAO.getWorlds();
		worldsView.setWorlds(worlds);
	}
	
	public void start( Container container, Object constraints ) {
		this.container = container;
		this.constraints = constraints;
		showWorldsView();
	}
	
	public void showWorldsView() {
		// TODO : this should be threaded
		// TODO : there should be a loading screen
		WorldDAO worldDAO = this.terrainDAOManager.getWorldDAO();
		List<World> worlds = worldDAO.getWorlds();
		this.worldsView.init();
		this.worldsView.setWorlds(worlds);
		this.container.removeAll();
		this.container.add( this.worldsView.getComponent(), this.constraints );
		this.container.validate();
	}
	
	public void showWorld( World world ) {

		this.currentWorld = world;
		// TODO : should deserialize event queue
		world.setEventQueue( new InMemoryEventQueue() );

		terrainView.setCurrentWorld(world);

		this.container.removeAll();
		this.container.add( terrainView, this.constraints );
		this.container.validate();
		terrainView.requestFocus();
	}

	@Override
	public void close() {
		super.close();
		if( this.currentWorld != null ) {
			this.terrainDAOManager.getWorldDAO().storeWorld(this.currentWorld);
			this.currentWorld = null;
		}
	}
	
	
}
